﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Collections;
using System.Diagnostics;

namespace ReBiaoDTU
{
    public static class ReBiaoPick
    {
        public static Hashtable rxDataTable;
        public static DTURunInfo f = null;
        static string serv_ip;
        static int serv_port;
        static bool bThreadRun=false;
        public static bool bServerStart = false;
        static Thread revThread;
        public static bool InitServer(string ip, int port)
        {
            if (f == null)
            {
                f = new DTURunInfo();
                f.Show();
            }
            serv_ip = ip;
            serv_port = port;
            HongDianDTU.SetCustomIP(HongDianDTU.inet_addr(serv_ip));
            HongDianDTU.SetWorkMode(0);//0-阻塞,支持多线程开发
            HongDianDTU.SelectProtocol(1);//开发包函数，设置服务类型：0-UDP，1-TCP
            StringBuilder mess = new StringBuilder(1000);
            int sign = HongDianDTU.start_net_service(IntPtr.Zero, 0, serv_port, mess);//开发包函数，非消息模式启动服务
            if (sign == 0)
            {
                f.ExternShowRx("*********DTU服务启动成功:" + serv_ip + ":" + serv_port.ToString() + "*********");
            }
            else
            {
                f.ExternShowRx("----------DTU服务启动失败:" + serv_ip + " " + serv_port.ToString() + "-----原因:"+mess.ToString());
                return false;
            }
            bThreadRun = true;
            revThread = new Thread(new ThreadStart(DTUProc));//创建新的阻塞模式下读取数据线程
            revThread.Start(); //启动阻塞模式下读取数据线程
            rxDataTable = Hashtable.Synchronized(new Hashtable());
            bServerStart = true;
            return true;
        }
        public static bool StopSever()
        {
            StringBuilder mess = new StringBuilder(100);
            //停止服务
            //stop_gprs_server(mess);			
            HongDianDTU.stop_net_service(mess);
            f.ExternShowRx(mess.ToString());
            return true;
        }
        static void GetReBiaoData()
        {

        }
        //********************************************************
        //阻塞模式下线程处理函数
        //********************************************************
        static void DTUProc()
        {
            GPRS_DATA_RECORD recdPtr = new GPRS_DATA_RECORD();
            StringBuilder mess = new StringBuilder(100);
            GPRS_USER_INFO user_info = new GPRS_USER_INFO();

            //读取DTU数据				
            while (bThreadRun)
            {
//                 if (recvdata)
//                     continue;
                if (bThreadRun == false)
                    break;
                if (HongDianDTU.do_read_proc(ref recdPtr, mess, false) >= 0)
                {
                    //byte a = recdPtr.m_data_type;
                    //recvdata = true;
                    switch (recdPtr.m_data_type)
                    {
                        case 0x00:
                            break;
                        case 0x01:	//注册包												
                            if (HongDianDTU.get_user_info(recdPtr.m_userid, ref user_info) == 0)
                            {
                                //已经注册过	
                                //f.ExternShowRx(recdPtr.m_userid + "---心跳");
                                f.ExternListShowRx(user_info, recdPtr,recdPtr.m_data_type);
                                //没有注册过
                            }
                            break;
                        case 0x02:	//注销包
                            f.ExternShowRx(recdPtr.m_userid + "---注销");
                            f.ExternListShowRx(user_info, recdPtr, recdPtr.m_data_type);
                            break;
//                         case 0x04:	//无效包
//                             break;
                        case 0x09:	//数据包
                            f.ExternShowRx(recdPtr.m_userid + "---收到数据:"+stringfunc.byteToHexStr(recdPtr.m_data_buf,0,recdPtr.m_data_len," "));
                            List<GPRS_DATA_RECORD> rxList = (List<GPRS_DATA_RECORD>)rxDataTable[recdPtr.m_userid];
                            if (rxList == null)
                            {
                                rxList = new List<GPRS_DATA_RECORD>();
                            }
                            rxList.Add(recdPtr);
                            rxDataTable[recdPtr.m_userid] = rxList;
//                             if (checkBox1.Checked)
//                                 AddText("\r\n" + recdPtr.m_userid + "---" + recdPtr.m_recv_date + "---" + recdPtr.m_data_len.ToString() + "\r\n"
//                                     + StrToHex(recdPtr.m_data_buf, recdPtr.m_data_len) + "\r\n");
//                             else
//                                 AddText("\r\n" + recdPtr.m_userid + "---" + recdPtr.m_recv_date + "---" + recdPtr.m_data_len.ToString() + "\r\n"
//                                     + System.Text.Encoding.Default.GetString(recdPtr.m_data_buf) + "\r\n");
                            break;
//                         case 0x0d:
//                             AddText("\r" + recdPtr.m_userid + "---参数设置成功" + "\r\n");
//                             config.timer3.Enabled = false;
//                             config.button2.Enabled = true;
//                             MessageBox.Show("参数设置成功", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
//                             break;

//                         case 0x0b:
//                             AddText("\r" + recdPtr.m_userid + "---参数查询成功" + "\r\n");
//                             config.readconf();
//                             config.timer2.Enabled = false;
// 
//                             config.button1.Enabled = true;
//                             MessageBox.Show("参数查询成功", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
//                             break;
                        case 0x06:
                            f.ExternShowRx(recdPtr.m_userid + "---断开PPP连接成功");
                            break;
                        case 0x07:
                            f.ExternShowRx(recdPtr.m_userid + "---停止向DSC发送数据");
                            break;
                        case 0x08:
                            f.ExternShowRx(recdPtr.m_userid + "---允许向DSC发送数据");
                            break;
                        case 0x0A:
                            f.ExternShowRx(recdPtr.m_userid + "---丢弃用户数据");
                            break;
                        default:
                            f.ExternShowRx(recdPtr.m_userid + ":" + recdPtr.m_data_type + "未处理数据类型");
                            break;
                    }
                    //recvdata = false;
                }
                while (true)
                {
                    foreach (DictionaryEntry de in rxDataTable)
                    {
                        List<GPRS_DATA_RECORD> rxList = (List<GPRS_DATA_RECORD>)de.Value;
                        while (true)
                        {
                            for(int i=0;i<rxList.Count;i++)
                            {
                                GPRS_DATA_RECORD recd = rxList[i];
                                DateTime rxdt = Convert.ToDateTime(recd.m_recv_date);
                                if (rxdt.AddSeconds(120) < DateTime.Now)
                                {
                                    Debug.WriteLine("丢弃历史数据" + recd.m_recv_date);
                                    rxList.RemoveAt(i);
                                    continue;
                                }                                
                            }
                            break;
                        }                        
                    }
                    break;
                }
            }
            //	thread.Abort();
        }
    }
}
